<template>
  <!-- 表单 -->
  <el-form ref="formDataRef" label-width="120px" :model="formData" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请选择" value="">
        <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" style="width:80%" placeholder="请选择部门" @focus="dofocus" />
      <div class="tree-box">
        <el-tree v-if="isShow" :data="departmentList" :props="{ label: 'name' }" @node-click="hNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button>取消</el-button>
      <el-button type="primary" @click="doAdd">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
import { getdepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  name: 'EmpDialog',
  data() {
    return {
      // 表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
          { pattern: /^\S{1,4}$/, message: '姓名必须是1-4位的非空字符', trigger: 'blur' }],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对', trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '必填', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '工号必须是1-10位的非空字符',
            trigger: 'blur'
          }],
        departmentName: [{ required: true, trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      // 聘用形式数组
      hireType: EmployeeEnum.hireType,
      // 部门数据
      departmentList: [],
      // 部门弹框显示
      isShow: false
    }
  },
  methods: {
    // 获得焦点
    async dofocus() {
      try {
        const res = await getdepartment()
        // console.log(res)
        this.isShow = true
        res.data.data.depts.shift()
        this.departmentList = tranListToTreeData(res.data.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 用户在部门结构树上点击
    hNodeClick(data, node) {
    //   console.log('用户在部门结构树上点击', data, node)
      // 如果当前部门还有子部门，则不能选中
      if (data.children.length) {
        return
      }
      //   console.log('选中的是', data.name)
      // 1. 把当前选中的显示在input框中
      this.formData.departmentName = data.name
      // 2. 隐藏tree
      this.isShow = false
    },
    // 确定按钮
    doAdd() {
      this.$refs.formDataRef.validate(async valid => {
        if (!valid) return
        try {
          await addEmployee(this.formData)
          this.$message.success('员工新增成功')
          this.$emit('success')
        } catch (error) {
          console.log(error)
        }
      })
    },
    // 清空表单
    formReset() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.formDataRef.resetFields()
    }
  }
}
</script>
